$(function () {
    //客户加载
    $("[name='syltj_kh']").qyjgzcombobox();
    // 表格初始加载
    $('#syltj_grid').datagrid({
        url: $.webPath() + "/fkyj/yjbb/syltjList",
        toolbar: "#syltj_toolbar",
        fitColumns: true,
        singleSelect: true,
        pagination: true,
        pageNumber: 1,
        showFooter: true,
        rownumbers: true,
        fit:true,
        striped: true,
        columns: [
            [
                {field: 'id', title: 'id', hidden: true},
                {field: 'sbgsId', title: '客户id', hidden: true},
                {field: 'yq', title: '园区', hidden: true},
                {field: 'kh', title: '客户', align: 'center', width: 70},
                {field: 'syl', title: '损益率', align: 'center', width: 70,
                    formatter: function (value, row) {
                        if (value && row.id && row.id > 0) {
                            return  "<a title='点击查看详细'style='color:red;font-weight:bold;' href='javascript:syl_detail(" + row.id + ");'>" + value + '%' + "</a>";
                        }
                    }
                },
                {field: 'jsrq', title: '日期', align: 'center', width: 70},
                {field: 'jzsyl', title: '预警值', align: 'center', width: 70,
                    formatter: function (value, row) {
                        if (value && value != 0) {
                            return value + "%";
                        }
                    }
                },
                {field: 'cz', title: '查看损益率走势图', align: 'center', width: 70,
                    formatter: function (value, row) {
                        return '<a title="点击查看客户图表" name="tbname" href="javascript:void(0)" onclick="syltj_chart(' + row.sbgsId + "," + row.jzsyl + "," + "0" + ')"></a>'
                    }
                }
            ]
        ],
        onLoadSuccess: function () {
            $("a[name='tbname']").linkbutton({
                plain: true,
                iconCls: 'icon-chart'
            });
        }
    });
});

function syltj_chart1() {
    var khChart = $("#khid").val();
    syltj_chart(khChart, 1, "1");
}

function allSyltj_chart() {
    syltj_chart_zhu();
}

function syltj_chart_zhu() {
    $.ajax({
        type: "POST",
        url: $.webPath() + "/fkyj/yjbb/allSyltjChart",
        dataType: "text",
        beforeSend: function () {
            $.messager.progress();
        },
        success: function (result) {
            var dataJson = eval("(" + result + ")");
            $.messager.progress("close");
            if (null != dataJson.listXdata && null != dataJson.listYdata) {
                $('#syltj_container_zhu').highcharts({
                    chart: {
                        type: 'spline'
                    },
                    title: {
                        text: '客户损益率走势图',
                        x: -20
                    },
                    xAxis: {
                        categories: dataJson.listXdata
                    },
                    yAxis: {
                        title: {
                            text: '损益率(%)'
                        }
                    },
                    tooltip: {
                        valueSuffix: '%'
                    },
                    legend: {
                        layout: 'vertical',
                        align: 'right',
                        verticalAlign: 'middle',
                        borderWidth: 0
                    },
                    series: dataJson.listYdata
                });
            }
        }
    });
    document.getElementById("layout_syltj").style.display = "none"
    document.getElementById("div_syltj_container_zhu").style.display = "";
    document.getElementById("div_syltj_container").style.display = "none";
}

// 损益率图表
function syltj_chart(khChart, jzsyl, type) {
    if (type == "0") {
        if (!jzsyl) {
            $.messager.alert("提示", "预警值为空，请在损益率维护菜单中维护客户预警值！", "warning");
            return;
        }
    }
    if (khChart) {
        $("#khid").val(khChart);
    }
    var tjStart = $('#syltj_tj_start').datebox('getValue');
    var tjEnd = $('#syltj_tj_end').datebox('getValue');
    $.ajax({
        type: "POST",
        url: $.webPath() + "/fkyj/yjbb/syltjChart",
        dataType: "text",
        data: {khChart: khChart, tjStart: tjStart, tjEnd: tjEnd},
        beforeSend: function () {
            $.messager.progress();
        },
        success: function (result) {
            var dataJson = eval("(" + result + ")");
            $.messager.progress("close");
            if (dataJson.tipInfo == '1') {
                $.messager.alert("提示", "结束日期不能为空！", "warning");
            } else if (dataJson.tipInfo == '2') {
                $.messager.alert("提示", "开始日期不能为空！", "warning");
            } else if (dataJson.tipInfo == '3') {
                $.messager.alert("提示", "结束日期和开始日期之差不能超过30天！", "warning");
            } else {
                if (null != dataJson.listXdata && null != dataJson.listYdata && null != dataJson.listBsdata) {
                    $('#syltj_container').highcharts({
                        chart: {
                            type: 'spline'
                        },
                        title: {
                            text: '客户' + dataJson.listTitledata + '损益率走势图',
                            x: -20 //center
                        },
                        xAxis: {
                            categories: dataJson.listXdata
                        },
                        yAxis: {
                            title: {
                                text: '损益率(%)'
                            },
                            max: dataJson.maxValue,
                            plotLines: [
                                {
                                    color: 'red',
                                    label: {
                                        align: 'left',
                                        style: {
                                            color: '#000000',
                                            fontWeight: 'bold'
                                        },
                                        text: '预警值为：' + dataJson.listBsdata + "%",
                                        x: 10
                                    },
                                    dashStyle: 'solid',
                                    value: dataJson.listBsdata,
                                    width: 2
                                }
                            ]
                        },
                        tooltip: {
                            valueSuffix: '%'
                        },
                        legend: {
                            layout: 'vertical',
                            align: 'right',
                            verticalAlign: 'middle',
                            borderWidth: 0
                        },
                        series: dataJson.listYdata
                    });
                }
            }

        }
    });
    document.getElementById("layout_syltj").style.display = "none"
    document.getElementById("div_syltj_container").style.display = "";
    document.getElementById("div_syltj_container_zhu").style.display = "none";
}

// 损益率列表
function syltj_list() {
    document.getElementById("layout_syltj").style.display = ""
    document.getElementById("div_syltj_container").style.display = "none";
    document.getElementById("div_syltj_container_zhu").style.display = "none";
}

// 损益率查询
function syltj_search() {
    var kh = $("#syltj_toolbar input[name='syltj_kh']").val();
//    var syl = $("#syltj_toolbar input[name='syltj_syl']").val();
    var jsrqStart = $('#syltj_jsrq_start').datebox('getValue');
    var jsrqEnd = $('#syltj_jsrq_end').datebox('getValue');
    $('#syltj_grid').datagrid('load',
        {
            kh: kh,
//            syl: syl,
            jsrqStart: jsrqStart,
            jsrqEnd: jsrqEnd
        }
    );
}

// 查看入库明细
function syl_detail(sylId) {
    var button
    button = [
    ];
    $.window({
        title: "损益率详细",
        iconCls: 'icon-tip',
        url: $.webPath() + "/fkyj/yjbb/syl_detail?sylId=" + sylId,
        width: 700,
        height: 500,
        buttons: button
    });
}
